package eu.jpereira.trainings.designpatterns.structural.decorator.channel.decorator;

import static org.junit.Assert.assertEquals;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import eu.jpereira.trainings.designpatterns.structural.decorator.channel.SocialChannel;
import eu.jpereira.trainings.designpatterns.structural.decorator.channel.SocialChannelBuilder;
import eu.jpereira.trainings.designpatterns.structural.decorator.channel.SocialChannelProperties;
import eu.jpereira.trainings.designpatterns.structural.decorator.channel.SocialChannelPropertyKey;
import eu.jpereira.trainings.designpatterns.structural.decorator.channel.spy.TestSpySocialChannel;
import eu.jpereira.trainings.designpatterns.structural.decorator.channel.spy.TestSpySocialChannelBuilder;

public class ChainCensorDecoratorTest {

	@Before
	public void setUp() throws Exception {
	}

	@After
	public void tearDown() throws Exception {
	}

	@Test
	public void testChainThreeDecorators() {

		SocialChannelBuilder builder = new TestSpySocialChannelBuilder();
		SocialChannelProperties props = new SocialChannelProperties()
				.putProperty(SocialChannelPropertyKey.NAME,
						TestSpySocialChannel.NAME);

		SocialChannel channel = builder.with(new MessageTruncator(21))
				.with(new URLAppender("http://google.pl"))
				.with(new WordCensor()).getDecoratedChannel(props);

		channel.deliverMessage("What a stupid idea man");

		TestSpySocialChannel spyChannel = (TestSpySocialChannel) builder
				.buildChannel(props);

		assertEquals("What a ### idea... http://google.pl ",
				spyChannel.lastMessagePublished());

	}

	@Test
	public void testChainThreeDecoratorsWithoutBuilder() {

		SocialChannel channel = new TestSpySocialChannel();

		SocialChannel urlAppenderChannel = new URLAppender("http://google.pl",
				channel);

		SocialChannel messageTruncatorChannel = new MessageTruncator(18,
				urlAppenderChannel);

		SocialChannel wordCensorChannel = new WordCensor(
				messageTruncatorChannel);

		wordCensorChannel.deliverMessage("What a stupid idea man");

		TestSpySocialChannel spy = (TestSpySocialChannel) channel;

		assertEquals("What a ### idea... http://google.pl",
				spy.lastMessagePublished());
	}

	@Test
	public void testOtherChainThreeDecorators() {

		SocialChannelBuilder builder = new TestSpySocialChannelBuilder();

		SocialChannelProperties props = new SocialChannelProperties()
				.putProperty(SocialChannelPropertyKey.NAME,
						TestSpySocialChannel.NAME);

		SocialChannel channel = builder
				.with(new URLAppender("http://google.pl"))
				.andWith(new MessageTruncator(30)).with(new WordCensor())
				.getDecoratedChannel(props);

		channel.deliverMessage("stupid donkey idiot");

		TestSpySocialChannel spyChannel = (TestSpySocialChannel) builder
				.buildChannel(props);
		System.out.println(spyChannel.lastMessagePublished());
		assertEquals("### ### ### http://... ",
				spyChannel.lastMessagePublished());
	}
}
